package at.htlkaindorf.jsf.view;

import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import at.htlkaindorf.jsf.session.SessionBean;

/**
 * @author david.tomes
 */
@ManagedBean(name = "loginBean")
@ViewScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 2352214405736693726L;

    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    private String username;
    private String password;

    @PostConstruct
    public void init() {
        if (sessionBean.getUsername() != null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("guestbookOverview.xhtml");
            } catch (IOException e) {
                // TODO: Logging of Java IOException.
            }
        }
    }

    public String login() {
        sessionBean.setUsername(username);

        // Navigation to page guestbookOverview.xhtml.
        return "guestbookOverview";
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
